package net.cmkj.mine.bean;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Set;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.tx.Tx;

import net.cmkj.mine.bean.base.BaseSysLog;
import net.cmkj.mine.common.model.Condition;
import net.cmkj.mine.common.model.InvokeResult;
import net.cmkj.mine.common.model.Operators;
import net.cmkj.mine.util.CommonUtils;

/**
 * Generated by JFinal.
 */
@SuppressWarnings("serial")
public class SysLog extends BaseSysLog<SysLog> {

	public static final SysLog dao = new SysLog().dao();
	
	private static Logger log = LogManager.getLogger(SysLog.class);
	
	/**
	 * 获取列表
	 * @param pageNumber
	 * @param pageSize
	 * @param name
	 * @param status
	 * @return
	 */
	public Page<SysLog> paginate(int pageNumber, int pageSize, String name, Integer status, String orderbyStr) throws Exception {
		log.info("获取列表");
		List<Object> params = new ArrayList<Object>();
		StringBuffer sqlExceptSelect=new StringBuffer("from sys_log t where 1=1");
		if(CommonUtils.isNotEmpty(name)){
			sqlExceptSelect.append(" and t.name like ?");
			params.add(name);
		}
		if(status != null){
			sqlExceptSelect.append(" and t.status=?");
			params.add(status);
		}
		sqlExceptSelect.append(" "+orderbyStr);
		return dao.paginate(pageNumber, pageSize, "SELECT t.* ", sqlExceptSelect.toString(), CommonUtils.listToArr(params));
	}
	
	public Page<SysLog> paginate(int pageNumber, int pageSize, String classname, LinkedHashMap<String,String> orderbyStr) throws Exception {
		log.info("获取系统日志列表");
		Set<Condition> conditions=new HashSet<Condition>();
		if(CommonUtils.isNotEmpty(classname)){
			conditions.add(new Condition("class_name", Operators.LIKE, classname));
		}
		return this.getPage(pageNumber, pageSize, conditions, orderbyStr);
	}
	
	/**
	 * 获取对象
	 * @param id
	 * @return
	 */
	public SysLog findById(long id) throws Exception {
		log.info("获取对象");
		return dao.findFirst("select * from sys_log where id=?", id);
	}
	
	public void saveSysLog(Integer uid,String ip,String from,String url,String className,String methodName,long startTime,int errCode,String errMsg,String params){
		SysLog sysLog = new SysLog();
		sysLog.setUid(uid);
		sysLog.setIp(ip);
		sysLog.setFroms(from);
		sysLog.setUrl(url);
		sysLog.setClassName(className);
		sysLog.setMethodName(methodName);
		sysLog.setErrCode(errCode);
		sysLog.setErrMsg(errMsg);
		sysLog.setStartTime(new Date(startTime));
		sysLog.setSpendTime(System.currentTimeMillis()-startTime);
		sysLog.setParams(params);
		sysLog.save();
	}
	
	/**
	 * 对象删除
	 * @param ids
	 * @return
	 */
	@Before(Tx.class)
	public InvokeResult deleteByIds(String ids) throws Exception{
		log.info("对象删除");
    	String idarr[] = ids.split(",");
    	for (String id : idarr) {
    		if (!dao.findById(id).delete()) {
    			log.error("批量删除失败！");
    			throw new RuntimeException(id+"批量删除失败");
    		}
    	}
    	return InvokeResult.success();
	}
	
	/**
	 * 修改对象状态
	 * @param ids
	 * @return
	 */
	@Before(Tx.class)
	public InvokeResult setStatus(String ids, Integer status) throws Exception{
		log.info("对象状态变更");
		String idarr[] = ids.split(",");
    	for(String id : idarr){
    		if (!dao.findById(id).set("status", status).update()) {
    			log.error("对象批量状态变更失败！");
    			throw new RuntimeException(id+"状态批量变更失败！");
    		}
		}
		return InvokeResult.success();
	}
}
